package xyz.erupt.flow.bean.entity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.support.spring.annotation.FastJsonFilter;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.*;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLDeleteAll;
import org.hibernate.annotations.Where;
import xyz.erupt.flow.bean.entity.form.FormComponent;
import xyz.erupt.flow.bean.entity.node.OaProcessNode;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @description: 工作流定义
 * @className: OaWorkFlowDefinition
 * @author: PP
 * @date: 2024/10/19 9:18
 * @Version: 1.0
 */
@Entity(name = "oa_work_flow_definition")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@SQLDelete(sql="update oa_work_flow_definition set is_delete=true where id=?")
@SQLDeleteAll(sql="update oa_work_flow_definition set is_delete=true where id=?")
@Where(clause = "is_delete = false")
public class OaWorkFlowDefinition extends OaBaseLogicDelete{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne
    @JoinColumn(name = "groupId")
    private OaWorkFlowGroup workFlowGroup;

    /**
     * @description: 表单/流程名称
     * @author: PP
     * @date: 2024/10/19 9:45
     * @param:
     * @return:
    **/
    private String formName;

    /**
     * 图标配置
     */
    private String logo;

    /**
     * 设置项
     */
    @Lob
    @Column//json类型
    private String settings;

    /**
     * 流程设置内容
     */
    @Lob
    @Column//json类型
    private String process;

    /**
     * 表单内容
     */
    @Lob
    @Column//json类型
    private String formItems;

    /**
     * 备注
     */
    private String remark;
    /**
     * 状态 0=正常 1=已停用
     */
    private boolean stop;

    /**
     * 创建/更新时间
     */
    private Date updateTime;

    private Integer sort;

    /**
     * 数据定义唯一键（多个修改版本中使用同一个流程键）
     */
    private String processKey;

    /**
     * 版本
     */
    private Long version;

    /**
     * @description:获取流程节点数据
     * @author: PP
     * @date: 2024/10/19 9:46
     * @param: []
     * @return: xyz.erupt.flow.bean.entity.node.OaProcessNode
    **/
    public OaProcessNode getProcessNode() {
        if(this.getProcess()==null) {
            return null;
        }
        return JSON.parseObject(this.getProcess(), OaProcessNode.class);
    }

    /**
     * @description:获取表单定义数据
     * @author: PP
     * @date: 2024/10/19 9:46
     * @param: []
     * @return: java.util.List<xyz.erupt.flow.bean.entity.form.FormComponent>
    **/
    public List<FormComponent> getFormItemsList(){
        if (this.getFormItems() == null){
            return null;
        }
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.readValue(this.getFormItems(),objectMapper.getTypeFactory().constructCollectionType(List.class,FormComponent.class));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
